﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com
 * Website  : http://www.wishenterprise.com
 * 
 * */

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using QWEntity;
using QWEntity.Local;
using QWRepository;
using QWRepository.Local;

namespace QWControlCenter
{
    /// <summary>
    /// Workflow user interface
    /// </summary>
    public partial class WorkflowUI : Form
    {
        /// <summary>
        /// ListViewColumnSorter
        /// </summary>
        private ListViewColumnSorter _lvwColumnSorter;

        /// <summary>
        /// saving mdoe
        /// </summary>
        private SaveMode _saveMode;

        /// <summary>
        /// Initializes a new instance of the <see cref="WorkflowUI"/> class.
        /// </summary>
        public WorkflowUI()
        {
            InitializeComponent();
            InitializeListViewColumnSorter();
            ShowAll();
            _saveMode = SaveMode.AddMode;
        }

        /// <summary>
        /// Initializes the list view column sorter.
        /// </summary>
        private void InitializeListViewColumnSorter()
        {
            _lvwColumnSorter = new ListViewColumnSorter();
            lsvData.ListViewItemSorter = _lvwColumnSorter;
        }

        /// <summary>
        /// Shows all.
        /// </summary>
        private void ShowAll()
        {
            FillDataGrid(new WorkflowBase().GetAll());
        }

        /// <summary>
        /// Fills the data grid.
        /// </summary>
        /// <param name="row">The row.</param>
        private void FillDataGrid(IEnumerable<Workflow> row)
        {
            lsvData.Items.Clear();
            foreach (Workflow r in row)
            {
                ListViewItem lvItem = lsvData.Items.Add(r.WorkflowId, r.WorkflowId, 0);
                lvItem.SubItems.Add(r.WorkflowDescription);
            }

            lsvData.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            EnvironmentRepository.PasangWarnaAlternatif(lsvData);
        }

        /// <summary>
        /// Resets the data.
        /// </summary>
        private void ResetData()
        {
            _saveMode = SaveMode.AddMode;
            ShowAll();
            txtID.Enabled = true;
            txtID.Clear();
            txtID.Focus();
            txtDescription.Clear();
        }

        /// <summary>
        /// Confirms the specified s message.
        /// </summary>
        /// <param name="sMessage">The s message.</param>
        /// <returns></returns>
        private DialogResult Confirm(string sMessage)
        {
            DialogResult dialogResult =
                (MessageBox.Show(sMessage, @"Confirm",
                                 MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question));
            switch (dialogResult)
            {
                case DialogResult.No:
                    ResetData();
                    break;
            }
            return dialogResult;
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var r = new Workflow
                            {
                                WorkflowId = txtID.Text.Trim(),
                                WorkflowDescription = txtDescription.Text.Trim()
                            };

                // --- Confirm
                if (Confirm("Do you want to save this data?") != DialogResult.Yes) return;

                using (var db = new WorkflowBase())
                {
                    if ((_saveMode == SaveMode.AddMode
                             ? db.Save(r) // Save New
                             : db.Update(r)) == 1) // Update
                        ResetData();
                }
            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, @"Warning!",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, @"Warning!",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        /// <summary>
        /// Handles the Validated event of the txtID control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtID_Validated(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtID.Text.Trim())) return;

            var db = new WorkflowBase();
            Workflow r = db.GetById(txtID.Text.Trim());
            db.Dispose();

            try
            {
                txtDescription.Text = r.WorkflowDescription;
                txtID.Enabled = false;
                _saveMode = SaveMode.EditMode;
            }
            catch (NullReferenceException)
            {
                _saveMode = SaveMode.AddMode;
            }
        }

        /// <summary>
        /// Handles the ItemActivate event of the lsvData control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void lsvData_ItemActivate(object sender, EventArgs e)
        {
            if ((lsvData.Items.Count <= 0) | (lsvData.SelectedItems.Count <= 0)) return;
            txtID.Text = lsvData.SelectedItems[0].Text.Trim();
            txtID_Validated(this, null);
        }

        /// <summary>
        /// Handles the ColumnClick event of the lsvData control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.ColumnClickEventArgs"/> instance containing the event data.</param>
        private void lsvData_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == _lvwColumnSorter.SortColumn)
            {
                _lvwColumnSorter.Order = _lvwColumnSorter.Order == SortOrder.Ascending
                                             ? SortOrder.Descending
                                             : SortOrder.Ascending;
            }
            else
            {
                _lvwColumnSorter.SortColumn = e.Column;
                _lvwColumnSorter.Order = SortOrder.Ascending;
            }

            lsvData.Sort();
            EnvironmentRepository.PasangWarnaAlternatif(lsvData);

        }

        /// <summary>
        /// Handles the Click event of the btnDelete control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnDelete_Click(object sender, EventArgs e)
        {

            // --- If Not EditMode
            if (_saveMode != SaveMode.EditMode) return;

            try
            {
                var r = new Workflow
                            {
                                WorkflowId = txtID.Text.Trim()
                            };

                var db = new WorkflowBase();

                if (Confirm("Are you sure to delete this data?") == DialogResult.Yes)
                {
                    DialogResult dlg;

                    using (var f = new LogonPresentation())
                    {
                        dlg = f.ShowDialog(this);
                    }

                    if (dlg == DialogResult.OK)
                    {
                        if ((db.Delete(r) == 1)) ResetData();
                    }
                }

                db.Dispose();

            }
            catch (ArgumentNullException ex)
            {
                MessageBox.Show(ex.Message, @"Warning!",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, @"Warning!",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        /// <summary>
        /// Handles the Click event of the btnClear control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnClear_Click(object sender, EventArgs e)
        {
            ResetData();
        }

        /// <summary>
        /// Handles the TextChanged event of the txtID control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtID_TextChanged(object sender, EventArgs e)
        {
            ((TextBox) sender).Text = EnvironmentRepository.SuppressChar(((TextBox) sender).Text);
        }

        /// <summary>
        /// Handles the TextChanged event of the txtDescription control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtDescription_TextChanged(object sender, EventArgs e)
        {
            ((TextBox) sender).Text = EnvironmentRepository.SuppressChar(((TextBox) sender).Text);
        }
    }
}